<template>
	<view class="">
		<nav-bar title="地址管理"></nav-bar>
		<van-address-list @select="selectAddress" v-model="chosenAddressId" :list="list" default-tag-text="默认" @add="onAdd" @edit="onEdit" />
	</view>
</template>

<script>
	const Address = uniCloud.importObject("address");
	import Storage from '@/untils/storage.js'
	import {ref} from 'vue'
	export default {
		setup() {
			return {
				chosenAddressId:ref(""),
			};
		},
		data(){
			return{
				list:[]
			}
		},
		onLoad() {
				this.getAddressList();
		},
		methods:{
			onAdd(){
				uni.navigateTo({
					url:"/pages/user/address/add"
				})
			},
			getAddressList(){
				Address.list(Storage.userinfo._id).then(res=>{
					// console.log(res);
					let list = res.data
					for(let i =0;i<list.length;i++){
						list[i].id = list[i]._id;
						if(list[i].is_default){
							this.chosenAddressId = list[i]._id
						}
					}
					this.list = list
				})
			},
			onEdit(e){
				// console.log(e);
				uni.navigateTo({
					url:"/pages/user/address/edit?id="+e.id
				})
			},
			selectAddress(e){
				// console.log(e);
				this.chosenAddressId = e.id
				Address.setDefault(e.user_id,e.id).then(res=>{
					// console.log(res);
					this.$store.commit("setDefaultAddress",e)
					console.log(this.$store.state.address.defaultAddress);
				})
			}
		}
	};
</script>

<style>
</style>
